﻿using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PlayNote.Bll;
using PlayNote.Model;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.Json.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlayNote
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //readonly ObservableCollection<NoteTable> noteTables = new ObservableCollection<NoteTable>();
        NoteTableViewModel vModel = new();
        public MainWindow()
        {
            InitializeComponent();
            DataContext = vModel;
            this.Closed += (s,e) =>
            {
                foreach (var item in windows)
                {
                    item.Close();
                }
            };
            //list.ItemsSource= NoteTableBll.GetAll();
            //NoteTableBll.GetAll().ForEach(m=> { if (noteTables.FirstOrDefault(n => n.Id == m.Id) == null) { noteTables.Add(m); } });
        }
        List<TipWin> windows = new List<TipWin>();
        private void Test_Click(object sender, RoutedEventArgs e)
        {
            //var list = NoteTableBll.GetAll(true);
            //this.ShowMsg("play");
            //bool b = Bll.NoteTableBll.InsertNote("Play");
            //Bll.NoteTableBll.DeleteNote(1);
            //NoteTableBll.UpdateNote(1, "PlayUpdate");
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //if(openFileDialog.ShowDialog() == true)
            //{
            //    string fileName=openFileDialog.FileName;
            //    var str=System.IO.File.ReadAllText(fileName);
            //    var temp=JsonConvert.DeserializeObject<JObject>(str);
            //    Dictionary<string, object> dic1 = new Dictionary<string, object>();
            //    foreach (var item in temp)
            //    {
            //        //item.Key
            //        dic1.Add(item.Key, item.Value);  
            //    }
            //}
            string str = null;

        }
        bool isPressShift = false;

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (sender is TextBox textBox)
            {
                if (e.Key == Key.Enter)
                {
                    if (isPressShift)
                    {
                        textBox.Text += "\n";
                        //设置光标的位置到文本尾
                        textBox.Select(textBox.Text.Length, 0);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(textBox.Text)) 
                        {
                            if (NoteTableBll.InsertNote(textBox.Text))
                            {
                                vModel.Refrsh();
                                //NoteTableBll.GetAll().ForEach(m => { if (noteTables.FirstOrDefault(n => n.Id == m.Id) == null) { noteTables.Add(m); } });
                            }
                        }
                    }
                }
                if (e.Key == Key.LeftShift)
                {
                    isPressShift = true;
                }
            }
                
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftShift)
            {
                isPressShift = false;
            }
        }

        private void Del_Click(object sender, RoutedEventArgs e)
        {
            if (sender is FrameworkElement element && element.DataContext is NoteTable note)
            {
                bool success = NoteTableBll.DeleteNote(note.Id);
                if (success)
                {
                    var n = vModel.NoteTables.FirstOrDefault(m => m.Id == note.Id);
                    if(n != null)
                        vModel.NoteTables.Remove(n);
                }
            }
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}